package com.wwwarehouse.taskcenter.fragment.invite_business_information;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.InviteRankFilterBean;
import com.wwwarehouse.taskcenter.bean.InviteRankOrderBean;
import com.wwwarehouse.taskcenter.bean.InviterCountBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/10 19:39
 * @描述 ${TODO}
 */
@Route(path = TaskCenterConstant.InviteCountFragment)
public class InviteCountFragment extends BaseFragment {

    private View                       mView;
    private CircleImageView            mCivCount;
    private TextView                   mTvRegistered;
    private TextView                   mTvNoPass;
    private TextView                   mTvRejectRegister;
    private TextView                   mTvRejectInvite;
    private TextView                   mTvWaiteCheck;
    private TextView                   mTvWaiteRegister;
    private TextView                   mTvWaiteAgreeInvite;
    private boolean                    isDay;
    private boolean                    isWeek;
    private boolean                    isMonth;
    private boolean                    isYear;
    private InviteRankFilterBean       mMInviteRankFilterBean1;
    private InviteRankFilterBean       mMInviteRankFilterBean2;
    private InviteRankFilterBean       mMInviteRankFilterBean3;
    private InviteRankFilterBean       mMInviteRankFilterBean4;
    private InviteRankFilterBean       mMInviteRankFilterBean5;
    private InviteRankFilterBean       mMInviteRankFilterBean6;
    private List<InviteRankFilterBean> mInviteRankFilterList;
    private Map                        mInviteRankingMap;
    private List<InviteRankOrderBean>  mOrderList;
    private InviteRankOrderBean        mInviteRankOrderBean;
    private List<String>               mSelectsList;
    private StateLayout                mStateLayout;
    private InviteRankFilterBean       mInviteRankFilterBean7;
    private List<String>               mInviteIdValueList7;
    private TextView                   mTvName;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_invite_count, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mStateLayout = findView(mView, R.id.sl);
        mTvName = findView(mView, R.id.tv_name);
        //头像
        mCivCount = findView(mView, R.id.civ_count);
        //已注册
        mTvRegistered = findView(mView, R.id.tv_register_count);
        //审核未通过
        mTvNoPass = findView(mView, R.id.tv_no_pass_count);
        //注册被拒绝
        mTvRejectRegister = findView(mView, R.id.tv_reject_register_count);
        //拒绝邀请
        mTvRejectInvite = findView(mView, R.id.tv_reject_invite_count);
        //待审核
        mTvWaiteCheck = findView(mView, R.id.tv_wait_check_count);
        //待注册
        mTvWaiteRegister = findView(mView, R.id.tv_wait_register_count);
        //待同意邀请
        mTvWaiteAgreeInvite = findView(mView, R.id.tv_wait_agree_invite_count);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof InviteCountFragment) {
            mActivity.setTitle("邀约详情统计");
        }
    }

    @Override
    public void requestDatas() {
        mInviteRankFilterList = new ArrayList<>();//筛选条件的集合
        mInviteRankingMap = new HashMap();//请求数据的map
        getInviteRankOrderBean("asc");//升降序
        getSelectsList();//最后一个集合

        mInviteRankFilterBean7 = new InviteRankFilterBean();
        mInviteRankFilterBean7.setType("in");
        mInviteRankFilterBean7.setColumn("inviter_id");
        mInviteIdValueList7 = new ArrayList<>();
        Bundle bundle = getArguments();
        //day
        if (bundle != null && bundle.getSerializable("mInviteRankFilterBean1") != null && bundle.getSerializable("mInviteRankFilterBean2") != null) {
            isDay = true;
            mMInviteRankFilterBean1 = (InviteRankFilterBean) bundle.getSerializable("mInviteRankFilterBean1");
            mMInviteRankFilterBean2 = (InviteRankFilterBean) bundle.getSerializable("mInviteRankFilterBean2");
            int inviterId = bundle.getInt("inviterId");

            mInviteIdValueList7.add(inviterId + "");
            mInviteRankFilterBean7.setValues(mInviteIdValueList7);
            mInviteRankFilterList.clear();
            mInviteRankFilterList.add(mMInviteRankFilterBean1);
            mInviteRankFilterList.add(mMInviteRankFilterBean2);
            mInviteRankFilterList.add(mInviteRankFilterBean7);
            mInviteRankingMap.put("dataSet", "inviter_day");

            getInviteCount();
            //week
        } else if (bundle != null && bundle.getSerializable("mInviteRankFilterBean1") != null && bundle.getSerializable("mInviteRankFilterBean3") != null && bundle.getSerializable("mInviteRankFilterBean4") != null) {

            isWeek = true;
            mMInviteRankFilterBean1 = (InviteRankFilterBean) bundle.getSerializable("mInviteRankFilterBean1");
            mMInviteRankFilterBean3 = (InviteRankFilterBean) bundle.getSerializable("mInviteRankFilterBean3");
            mMInviteRankFilterBean4 = (InviteRankFilterBean) bundle.getSerializable("mInviteRankFilterBean4");

            int inviterId = bundle.getInt("inviterId");
            mInviteIdValueList7.add(inviterId + "");
            mInviteRankFilterBean7.setValues(mInviteIdValueList7);
            //清一遍再添加 以保证每次请求都可以用这个集合
            mInviteRankFilterList.clear();
            mInviteRankFilterList.add(mMInviteRankFilterBean1);//暂时不传 石生未写好
            mInviteRankFilterList.add(mMInviteRankFilterBean3);
            mInviteRankFilterList.add(mMInviteRankFilterBean4);
            mInviteRankFilterList.add(mInviteRankFilterBean7);

            mInviteRankingMap.put("dataSet", "inviter_week");

            getInviteCount();
            //month
        } else if (bundle != null && bundle.getSerializable("mInviteRankFilterBean1") != null && bundle.getSerializable("mInviteRankFilterBean5") != null) {
            isMonth = true;
            mMInviteRankFilterBean1 = (InviteRankFilterBean) bundle.getSerializable("mInviteRankFilterBean1");
            mMInviteRankFilterBean5 = (InviteRankFilterBean) bundle.getSerializable("mInviteRankFilterBean5");
            int inviterId = bundle.getInt("inviterId");
            mInviteIdValueList7.add(inviterId + "");
            mInviteRankFilterBean7.setValues(mInviteIdValueList7);
            mInviteRankFilterList.clear();
            mInviteRankFilterList.add(mMInviteRankFilterBean1);
            mInviteRankFilterList.add(mMInviteRankFilterBean5);
            mInviteRankFilterList.add(mInviteRankFilterBean7);
            mInviteRankingMap.put("dataSet", "inviter_month");

            getInviteCount();
            //year
        } else if (bundle != null && bundle.getSerializable("mInviteRankFilterBean1") != null && bundle.getSerializable("mInviteRankFilterBean6") != null) {
            isYear = true;
            mMInviteRankFilterBean1 = (InviteRankFilterBean) bundle.getSerializable("mInviteRankFilterBean1");
            mMInviteRankFilterBean6 = (InviteRankFilterBean) bundle.getSerializable("mInviteRankFilterBean6");
            int inviterId = bundle.getInt("inviterId");
            mInviteIdValueList7.add(inviterId + "");
            mInviteRankFilterBean7.setValues(mInviteIdValueList7);
            mInviteRankFilterList.clear();
            mInviteRankFilterList.add(mMInviteRankFilterBean1);
            mInviteRankFilterList.add(mMInviteRankFilterBean6);
            mInviteRankFilterList.add(mInviteRankFilterBean7);
            mInviteRankingMap.put("dataSet", "inviter_year");

            getInviteCount();
        }


    }

    private void getInviteCount() {
        mInviteRankingMap.put("filters", mInviteRankFilterList);
        mInviteRankingMap.put("ifCount", "true");
        mInviteRankingMap.put("page", 1);
        mInviteRankingMap.put("size", 1);
        mInviteRankingMap.put("orders", mOrderList);
        mInviteRankingMap.put("selects", mSelectsList);
        mStateLayout.showProgressView(true);
        NoHttpUtils.httpPost(TaskCenterConstant.BI_GETDATA, mInviteRankingMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                switch (i) {
                    case 0:
                        if ("0".equals(commonClass.getCode())) {
                            InviterCountBean inviterCountBean = JSON.parseObject(commonClass.getData().toString(), InviterCountBean.class);
                            List<InviterCountBean.ListBean> list = inviterCountBean.getList();
                            InviterCountBean.ListBean listBean = list.get(0);
                            BaseApplication.getApplicationInstance().displayImg(listBean.getInviterUrl(), mCivCount);
                            mTvName.setText(listBean.getInviterName());//name
                            mTvRegistered.setText(listBean.getRegisterQty() + ""); //已注册
                            mTvNoPass.setText(listBean.getAuditFailed() + "");//审核未通过
                            mTvRejectRegister.setText(listBean.getRegisterDecline() + "");//注册被拒绝
                            mTvRejectInvite.setText(listBean.getInviteDecline() + "");//拒绝邀请
                            mTvWaiteCheck.setText(listBean.getAuditWaiting() + "");//待审核
                            mTvWaiteRegister.setText(listBean.getRegisterWaiting() + "");//待注册
                            mTvWaiteAgreeInvite.setText(listBean.getInviteAgree() + "");//待同意邀请
                        }
                        break;
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);//网络问题
            mStateLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getInviteCount();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    private void getInviteRankOrderBean(String s) {
        mOrderList = new ArrayList<>();
        mInviteRankOrderBean = new InviteRankOrderBean();
        mInviteRankOrderBean.setColumn("invite_ranking");
        mInviteRankOrderBean.setType(s);//升序
        mOrderList.add(mInviteRankOrderBean);
    }

    private void getSelectsList() {
        mSelectsList = new ArrayList<>();
        mSelectsList.add("inviter_id");
        mSelectsList.add("inviter_name");
        mSelectsList.add("inviter_url");
        mSelectsList.add("register_waiting");
        mSelectsList.add("invite_agree");
        mSelectsList.add("invite_decline");
        mSelectsList.add("audit_waiting");
        mSelectsList.add("audit_failed");
        mSelectsList.add("register_qty");
        mSelectsList.add("register_decline");
    }
}
